<template>
  <a-spin :spinning="cardLoading">
  
    <x-card v-if="hasPerm('slideshow:page')">
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="20">
            <a-col :md="5" :sm="24">
                <a-form-item label="">
                  <a-select allowClear v-model="queryParam.ShowRange" >
                    <a-select-option :value="1">家长端</a-select-option>
                    <a-select-option :value="2">园务端</a-select-option>
                  </a-select>
                </a-form-item>
            </a-col>
            <a-col  :md="5" :sm="24">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template slot="operator" v-if="hasPerm('slideshow:add')">
          <a-button @click="$refs.addForm.add(articlesubjectsInfos)" icon="plus" type="primary" v-if="hasPerm('slideshow:add')">新增</a-button>
          <x-down
            style="display:none"
            v-if="hasPerm('slideshow:export')"
            ref="batchExport"
            @batchExport="batchExport"
          />
        </template>
        <span slot="source" slot-scope="text, record">
          <span slot="source" >
            <span>{{record.source === 1? '内部':'外部'}}</span>
          </span>
        </span>
        <span slot="content" slot-scope="text, record">
          <span slot="content" >
            <span v-if="record.source === 1">{{record.contentTitle}}</span>
            <span v-else>{{record.httpURL}}</span>
          </span>
        </span>
        <span slot="image" slot-scope="text, record">
          <span slot="image" >
            <img class="preview-img" v-if="record.image" :src="imgTitle + record.image.filedId" alt="">
          </span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('slideshow:edit')" @click="$refs.editForm.edit(record, articlesubjectsInfos)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('slideshow:edit') & hasPerm('slideshow:delete')"/>
          <a-popconfirm v-if="hasPerm('slideshow:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </a-spin>
</template>

<script>
import { STable, XCard, XDown } from '@/components'
  import { articleList,  articlePage} from '@/api/modular/garden/articlemanage';
import { slideshowPage, slideshowDelete, getAllInternalResources } from '@/api/modular/slideshow/slideshowApi'
import addForm from './addForm'
import editForm from './editForm'

export default {
  components: {
    XDown,
    XCard,
    STable,
    addForm,
    editForm
  },

  data () {
    return {
      // 查询参数
      queryParam: {
        ShowRange: 1
      },
        cardLoading: false,
      // 表头
      columns: [
        {
          title: '标题',
          dataIndex: 'title'
        },
        {
          title: '内容来源',
          dataIndex: 'source',
          scopedSlots: { customRender: 'source' }
        },
        {
          title: '链接内容',
          dataIndex: 'content',
          scopedSlots: { customRender: 'content' }
        },
        {
          title: '缩略图',
          dataIndex: 'image',
          scopedSlots: { customRender: 'image' }
        },
        {
          title: '排序',
          dataIndex: 'sortValue',
        },
        {
          title: '发布时间',
          dataIndex: 'createdTime',
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return slideshowPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      imgTitle: '',
      articleList: [],
      classsubjects: [],
      articlesubjectsInfos: null,
    }
  },

  created () {
    this.imgTitle = process.env.VUE_APP_API_BASE_URL + '/sysFileInfo/previewImg?id=';
    if (this.hasPerm('slideshow:edit') || this.hasPerm('slideshow:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    // this.getarticleList();
    this.getDatas();
  },

  methods: {
    getDatas(){
      getAllInternalResources().then(res => {
        if (res.success) {
          this.articlesubjectsInfos = res.data;
        } else {
          this.$message.error('拉取失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('拉取错误' + err.message)
      })
    },
    getarticleList(){
      articlePage({pageNo: 1, pageSize: 1000}).then(res => {
        if (res.success) {
          this.articleList = res.data;
        } else {
          this.$message.error('拉取失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('拉取错误' + err.message)
      })
    },
    /**
     * 单个删除
     */
    singleDelete (record) {
      const param = [ record.id ]
      this.sysPosDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete () {
      const paramIds = this.selectedRowKeys.map((d) => {
        return  d 
      })
      this.sysPosDelete(paramIds)
    },
    /**
     * 删除
     */
    sysPosDelete (param) {
      slideshowDelete(param).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.clearRefreshSelected()
        } else {
          this.$message.error('删除失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.message)
      })
    },
    changeStatus (record) {
      console.log(record)
    },
    /**
     * 批量导出
     */
    batchExport () {
      // const paramIds = this.selectedRowKeys.map((d) => {
      //   return { 'id': d }
      // })
      // sysPosExport(paramIds).then((res) => {
      //   this.$refs.batchExport.downloadfile(res)
      // })
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }

}
</script>

<style lang="less" scoped>
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
.preview-img{
  display: inline-block;
  height: 50px;
}
</style>
